﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace VideoCoreWCF {
    public class FileHelper {
        // Video Paths will have the structure 
        // D:/CAM_DATA/[CameraIP]/[Date(MM-dd-yyyy)]/StartTime_EndTime.[avi]
        public const string VIDEO_PATH = @"E:\CAM_DATA\";
        private const string VIDEO_PATH_FORMAT = VIDEO_PATH + @"{0}\{1}\";
        public const string DATE_TIME_FORMAT = "{0:MM-dd-yyyy}";
        private FileHelper() {

        }
        /// <summary>
        /// Gets the full name of the file.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static string GetFullFileName(string name) {
            return VIDEO_PATH + name;
        }

        /// <summary>
        /// Return short file name in format /CameraIP/Date/startTime_endTime.extension
        /// [ex:03452012_03502012.avi]
        /// </summary>
        /// <param name="cameraIP">The camera IP.</param>
        /// <param name="time">The time.</param>
        /// <returns></returns>
        public static string GetFile(string cameraIP, DateTime time, bool isLastFile) {
            string dicrectory = string.Format(VIDEO_PATH_FORMAT, cameraIP,
                                              string.Format(DATE_TIME_FORMAT, time));
            if (Directory.Exists(dicrectory)) {
                string[] paths = Directory.GetFiles(dicrectory);
                if (paths != null && paths.Length > 0) {
                    var fileList = paths.ToList();
                    fileList.Sort((x, y) => y.CompareTo(x));// Desc
                    if (isLastFile) {
                        var lst = fileList.FirstOrDefault();
                        return lst.Substring(VIDEO_PATH.Length);
                    }
                    string tick = string.Format("{0:HHmmss}", time);
                    foreach (string path in fileList) {
                        string p = path.Substring(path.LastIndexOf(@"\") + 1, path.LastIndexOf(".") - path.LastIndexOf(@"\") - 1);
                        if (p.CompareTo(tick) > 0) {
                            continue;
                        }
                        return path.Substring(VIDEO_PATH.Length);
                    }

                    // If we dont see this --> See in previous folder(the day before)
                    return fileList.LastOrDefault().Substring(VIDEO_PATH.Length);
                }
            }
            return string.Empty;
        }

        public static string GetNextFile(string IP, string fileName) {
            string dicrectory = VIDEO_PATH + fileName.Substring(0, fileName.LastIndexOf(@"\"));

            if (Directory.Exists(dicrectory)) {
                string[] paths = Directory.GetFiles(dicrectory);
                if (paths != null && paths.Length > 0) {
                    var fileList = paths.ToList();
                    fileList.Sort((x, y) => y.CompareTo(x));// Desc
                    string file = fileList[0];
                    foreach (string s in fileList) {
                        if (s.Contains(fileName)) {
                            return file.Substring(VIDEO_PATH.Length);
                        } else {
                            file = s;
                        }
                    }
                }
            }
            return string.Empty;
        }
    }

}
